<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="50%"
            height="50%"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
                <el-form-item label="" prop="id" v-show="false">
                    <el-input v-model="formData.id" placeholder="请输入" />
                </el-form-item>
                <!-- <el-form-item label="资源类型" prop="type" >
                    <el-radio-group v-model="formData.type">
                        <el-radio :label="'1'">富文本</el-radio>
                        <el-radio label="'2'">文档</el-radio>
                        <el-radio :label="'3'">图片</el-radio>
                        <el-radio :label="'4'">视频</el-radio>
                        <el-radio :label="'5'">音频</el-radio>
                        <el-radio :label="'6'">题目</el-radio>
                        <el-radio :label="'7'">原书答案</el-radio>
                    </el-radio-group>
                </el-form-item> -->
                <el-form-item label="资源标题" prop="title">
                    <el-input v-model="formData.title" placeholder="请输入资源标题" />
                </el-form-item>
                <el-form-item :label="formData.type == '6'?'题目id':'资源内容'" prop="content">
                    <el-input v-model="formData.content" placeholder="资源内容"/>
                    <material-picker v-model="formData.content"  type="file" :limit="1" v-if="formData.type=='2'"/>
                    <material-picker v-model="formData.content"  type="image" :limit="1" v-if="formData.type=='3'"/>
                    <material-picker v-model="formData.content"  type="video" :limit="1" v-if="formData.type=='4'"/>
                    <material-picker v-model="formData.content"  type="audio" :limit="1" v-if="formData.type=='5'"/>
                </el-form-item>
                <el-form-item label="link类型" prop="linkType" v-show="false">
                    <el-input v-model="formData.linkType" placeholder="请输入linkType" />
                </el-form-item>
                <el-form-item label="linkId" prop="linkId" v-show="false">
                    <el-input v-model="formData.linkId" placeholder="请输入linkId" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  resourceEdit, resourceAdd, resourceDetail } from '@/api/wisdbook/resource'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑资源' : '新增资源'
})

const formData = reactive({
    id: '',
    type: '',
    title: '',
    content: '',
    linkType: '',
    linkId: '',
})

const formRules = {
    linkType: [
        {
            required: true,
            message: '请选择link类型   0:mark  1:dir',
            trigger: ['blur']
        }
    ],
    linkId: [
        {
            required: true,
            message: '请输入linkId',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await resourceEdit(data) : await resourceAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}



const getDetail = async (row: Record<string, any>) => {
    const data = await resourceDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
